#include "settings.h"
#include "ui_settings.h"

Settings::Settings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Settings)
{
    ui->setupUi(this);

    this->setWindowTitle("Settings");
    //将所有用户显示在列表中
    UserManager userManager;
    std::vector<User> allUsers = userManager.getAllUsers();
    for (const User& user : allUsers) {
        qDebug() << "Username:" << user.username;
        ui->listWidget->addItem(user.username);
    }
    //键盘初始化
    ui->wifi->setFocus();
    keyboard->initFocusWidget(ui->widget);
    keyboard->hide();
    isKeyBoardShow = 0;
    //ui美化
    this->initStyleSheet();
    //close keyboard
    connect(keyboard,&frmKeyBoard::hiding,this,&Settings::hidekeyboard);
    //wifi intial NetworkManager
    system("sudo NetworkManager");
    system("sudo nmcli radio wifi on");
}

Settings::~Settings()
{
    delete ui;
}

void Settings::initStyleSheet()
{
    QString qss;

    qss += "QLineEdit{border-width: 1px; border-radius: 10px; border-style: solid; border-color: rgb(98,183,152); }";
    qss += "#password{border-color:rgb(193,150,144);}";
    /*
    qss += "QPushButton:hover{background-color:rgb(229,229,229); color:black;}";
    qss += "QPushButton:pressed,QPushButton:checked{background-color:rgb(0,118,215);}";
    qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
    qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
    qss += "#pushButton_space{min-width:500px;}";
    qss += "#pushButton_backspace,#pushButton_shift{min-width:100px;}";
    qss += "#pushButton_enter{min-width:120px;}";
    qss += "#pushButton_tab,#pushButton_ctrl{min-width:70px;}";
    qss += "#pushButton_capsLock{min-width:80px;}";
    qss += "#pushButton_up{min-width:150px;}";
    */
    qss += "QHeaderView::section {"
                   "background-color: black;"
                   "border: 1px solid gray;"
                   "height: 5px; /* 调整高度 */"
                   "}";
    this->setStyleSheet(qss);
}

void Settings::on_confirmdelete_clicked()
{
    //用户删除
    if(ui->listWidget->currentItem()->text() == "admin"){
        QMessageBox::warning(this,tr("delete failed"),tr("can not delete manager！"),QMessageBox::Ok);
    }else{
        UserManager userManager;
        bool deletUserResult = userManager.deleteUser(ui->listWidget->currentItem()->text());
        if(deletUserResult){
            QMessageBox::warning(this,tr("delete success"),tr("delete user success！"),QMessageBox::Ok);
            qDebug()<<"delete success";
        }else{
            QMessageBox::warning(this,tr("delete failed"),tr("delete user failed！"),QMessageBox::Ok);
            qDebug()<<"delete failed";
        }
    }

    resetListWidget();//刷新用户页面
}


void Settings::on_confirm_clicked()
{
    //wifi
    QString command = "sudo nmcli dev wifi connect " + ui->wifi->text(); +" password " + ui->password->text();
    if(-1 != system(command.toStdString().c_str())){
                                QNetworkConfigurationManager mgr;
                                if(mgr.isOnline()){             //wifi已连接
                                    qDebug("wifi is online!");
                                    QMessageBox::warning(this,tr("Connected"),tr("wifi activated！"),QMessageBox::Ok);
                                    emit wifi(1);
                                }
                                else{
                                    qDebug("wifi is offline!");
                                    QMessageBox::warning(this,tr("failed"),tr("failed to connect wifi！"),QMessageBox::Ok);
                                    emit wifi(0);
                                }
    }
}


void Settings::on_adduser_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(6);
}




void Settings::on_keyboard_clicked()
{
    if(isKeyBoardShow == 0){
        keyboard->setGeometry(0, 175, 431, 166);
        keyboard->show();
        isKeyBoardShow = 1;
    }else{
        keyboard->hide();
        isKeyBoardShow = 0;
    }
}


void Settings::on_home_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    Permission::LastPage = 4;
    emit display(5); //l2
}


void Settings::on_selectmethod_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(1); //s
}


void Settings::on_methoddevelop_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(3); //m
}

//更新用户列表
void Settings::resetListWidget()
{
    //重新将所有用户显示在列表中
    ui->listWidget->clear();
    UserManager userManager;
    std::vector<User> allUsers = userManager.getAllUsers();
    for (const User& user : allUsers) {
        //qDebug() << "Username:" << user.username;
        ui->listWidget->addItem(user.username);
    }
}

//更新药品列表
void Settings::resetListWidget_2()
{
    //将该用户所拥有的方法显示在列表中(更新列表)
    ui->listWidget_2->clear();
    ItemManager itemManager;
    QVector<Item> allItems;
    if(Permission::User == "admin"){
        allItems = itemManager.getAllItems();
    }else{
        allItems = itemManager.getAllItemsByUser(Permission::User);
    }

    for (const Item& item : allItems) {
        qDebug() << "Itemname:" << item.barcode;
        ui->listWidget_2->addItem(item.barcode);
    }
}

//通过列表删除某个药品
void Settings::on_confirmdelete_2_clicked()
{
    ItemManager itemManager;
    bool success = itemManager.deleteItem(ui->listWidget_2->currentItem()->text());
    resetListWidget_2();//刷新列表
    if (success) {
        QMessageBox::warning(this,tr("delete success"),tr("delete item success！"),QMessageBox::Ok);
        qDebug() << "Item deleted successfully";
    } else {
        QMessageBox::warning(this,tr("delete failed"),tr("delete item failed！"),QMessageBox::Ok);
        qDebug() << "Failed to delete Item";
    }
}

//通过输入barcode删除某个药品
void Settings::on_delete_2_clicked()
{
    ItemManager itemManager;
    bool success = itemManager.deleteItem(ui->barcode->text());
    resetListWidget_2();//刷新列表
    if (success) {
        QMessageBox::warning(this,tr("delete success"),tr("delete item success！"),QMessageBox::Ok);
        qDebug() << "Item deleted successfully";
    } else {
        QMessageBox::warning(this,tr("delete failed"),tr("delete item failed！"),QMessageBox::Ok);
        qDebug() << "Failed to delete Item";
    }
    //每次删除后清除
    ui->barcode->clear();
}

//hide keyboard by keyboard's button
void Settings::hidekeyboard()
{
    keyboard->hide();
    isKeyBoardShow = 0;
}
